package io.renren.modules.medical.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.modules.medical.dao.DictDistrictDao;
import io.renren.modules.medical.entity.DictDistrictEntity;
import io.renren.modules.medical.service.DictDistrictService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("dictDistrictService")
public class DictDistrictServiceImpl extends ServiceImpl<DictDistrictDao, DictDistrictEntity> implements DictDistrictService {

    @Override
    public List<DictDistrictEntity> getByParent(String parent) {
        LambdaQueryWrapper<DictDistrictEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(parent != null, DictDistrictEntity::getParent, parent);
        return this.list(queryWrapper);
    }

    @Override
    public String getAddress(String province, String city, String district) {
        String provinceName = getNameByCode(province);
        String cityName = getNameByCode(city);
        String districtName = getNameByCode(district);
        return provinceName + "/" + cityName + "/" + districtName;
    }

   public String getNameByCode(String code){
       LambdaQueryWrapper<DictDistrictEntity> queryWrapper = new LambdaQueryWrapper<>();
       queryWrapper.eq(StringUtils.isNotEmpty(code), DictDistrictEntity::getCode, code);
       return this.getOne(queryWrapper).getName();
   }

}